package Sort;


public class Reverse {

	public static void main(String[] args) {
		String str="Student. A Am I";
		reverseSentence(str);
	}
    public static void reverseSentence(String str){
    	if(str==null||str.length()==0){
    		return;
    	}
    	char[] ch = str.toCharArray();
    	reverse(ch,0,ch.length-1);
    	int start = 0 ;
    	int end = 0;
    	while(true){
    		if(ch[start]==' '){
    			start++;
    			end++;
    		}
    		else if(ch[end]==' '){
    			reverse(ch,start,end-1);
    			start=end+1;
    			end++;
    		}
    		else if(end==ch.length-1){
    			reverse(ch, start, end);
    			break;
    		}
    		else{
    			end++;
    		}
    	}
    	for(int i=0;i<ch.length;i++){
    		if(ch[i]>='a'&&ch[i]<='z'){
    		   ch[i] = (char) (ch[i]-32);
    		}
    		else if(ch[i]>='A'&&ch[i]<='Z'){
    			ch[i]=(char) (ch[i]+32);
    		}
    		else{
    			continue;
    		}
    	
    	}
    	for(int i=0;i<ch.length;i++)
        {
        	System.out.print(ch[i]);
        	
        }
    }
    public static void reverse(char[] ch,int start,int end){
    	if(start==end){
    		return;
    	}
    	while(start<end){
    		char temp = ch[start];
    		ch[start] = ch[end];
    		ch[end] = temp;
    		start++;
    		end--;
    	}
    }
}
